﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace ChoiceTree.ViewModels
{
    public class Decision
    {
        #region Constructors
        private Decision(int questionID) 
        {
            using (var ctx = new Models.TreeDataEntities())
            {
                var question = ctx.Questions.Where(q => q.questionID == questionID).FirstOrDefault();
                QuestionText = question.questionText;
                Choices = new List<DecisionChoice>();
                foreach (var choice in question.QuestionChoices)
                {
                    Choices.Add(new DecisionChoice()
                    {
                        ChoiceID = choice.Choice.choiceID,
                        ChoiceText = choice.Choice.choiceText,
                        NextQuestionID = choice.Choice.nextQuestionID
                    });
                }
            }
        }

        #endregion

        #region Factory Methods
        public static Decision GetDecisionFromTreeID(int treeID)
        {
            int questionID;
            using (var ctx = new Models.TreeDataEntities())
            {
                var tree = ctx.Trees.Where(i => i.treeID == treeID).FirstOrDefault();
                questionID = tree.startingQuestionID;
            }
            return new Decision(treeID);
        }
        public static Decision GetDecisionFromQuestionID(int questionID)
        {
            return new Decision(questionID);
        }
        #endregion

        #region Public Fields
        public int QuestionID;
        public string QuestionText;
        public List<DecisionChoice> Choices;
        #endregion

        #region PrivateMembers

        #endregion

    }
}